package com.wzdigit.wms.wms.service.inv;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.mes.basic.api.BasicDataItemDetailApi;
import com.wzdigit.mes.basic.dto.DataItemDetailDTO;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.inv.InvInfoRequest;
import com.wzdigit.wms.basic.client.response.basic.SkuBaseResponse;
import com.wzdigit.wms.basic.client.response.inv.report.InvInfoDtlResponse;
import com.wzdigit.wms.basic.client.response.inv.report.InvInfoMainResponse;
import com.wzdigit.wms.basic.client.response.inv.report.InvInfoSnResponse;
import com.wzdigit.wms.basic.domain.basic.Location;
import com.wzdigit.wms.basic.domain.inventory.Goods;
import com.wzdigit.wms.basic.enums.GoodsStatusEnum;
import com.wzdigit.wms.basic.enums.InvAttrEnum;
import com.wzdigit.wms.basic.enums.QaStatusEnum;
import com.wzdigit.wms.basic.mapper.core.basic.LocationMapper;
import com.wzdigit.wms.basic.mapper.core.basic.SkuMapper;
import com.wzdigit.wms.basic.mapper.core.inv.InvInfoMapper;
import com.wzdigit.wms.basic.mapper.core.inventory.GoodsMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 库存查询PDA
 * 前台
 * Created by zyf on 2021/7/12 11:37
 */
@Service
@Slf4j
public class InvInfoReportService {
    @Value(value = "${default_owner.code}")
    private String DEFAULT_OWNER;
    @Value(value = "${default_owner.name}")
    private String DEFAULT_OWNER_NAME;

    @Autowired
    private InvInfoMapper invInfoMapper;
    @Autowired
    private LocationMapper locationMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private SkuMapper skuMapper;

    @Reference(version = "${dubbo.consumers.basic.data.item.detail.api:1.0.0}", check = false, timeout = 300000)
    private BasicDataItemDetailApi dataItemDetailApi;

    //查询PDA主页
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public PagerInfo<List<InvInfoMainResponse>> searchMain(String goodsSn, String poNo,
                                                           String locCode, Boolean flag, Pager<?> pager){
        List<InvInfoMainResponse> nullList=new ArrayList<>();
        if (goodsSn==null&&poNo==null&&locCode==null){
            Page<InvInfoMainResponse> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
            PagerInfo<List<InvInfoMainResponse>> pagerInfo = new PagerInfo<>();
            pagerInfo.setTotalCount(page.getTotal());
            pagerInfo.setItems(nullList);
            return pagerInfo;
        }
        Integer locId=null;
        String  lotno=null;
        InvInfoRequest dto=null;
        //SN不为空，查询对应SN的批次号
        if (goodsSn!=null){
            Goods goods = goodsMapper.selectOne(new QueryWrapper<Goods>().eq("GOODS_SN", goodsSn));
            if (goods!=null){
                dto=new InvInfoRequest();
                dto.setSkuId(new BigDecimal(goods.getSkuId()));
                //查询物料描述
                SkuBaseResponse skuBaseResponse = skuMapper.selectOneBySkuId(goods.getSkuId());
                dto.setSkuDesc(skuBaseResponse.getDescCn());
                dto.setPoNo(goods.getPoNo());
                dto.setSoNo(goods.getSoNo());
                dto.setSoItem(goods.getSoItem());
                dto.setQaStatus(goods.getQaStatus());
                dto.setWhsId(goods.getWhsId());
                dto.setLocId(goods.getLocId());
                dto.setFactory(goods.getFactory());
                dto.setLotno(goods.getLotno());
                dto.setOwner(goods.getOwner());
                dto.setVendorId(goods.getVendorId());
                dto.setInvAttr(goods.getInvAttr());
            }
            if (goods==null){
                Page<InvInfoMainResponse> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
                PagerInfo<List<InvInfoMainResponse>> pagerInfo = new PagerInfo<>();
                pagerInfo.setTotalCount(page.getTotal());
                pagerInfo.setItems(nullList);
                return pagerInfo;
            }
        }
        //查询库位ID
        if (locCode!=null){
            QueryWrapper<Location> locationQueryWrapper=new QueryWrapper<>();
            locationQueryWrapper.eq("LOC_CODE",locCode);
            Location location = locationMapper.selectOne(locationQueryWrapper);
            if(location==null){
                Page<InvInfoMainResponse> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
                PagerInfo<List<InvInfoMainResponse>> pagerInfo = new PagerInfo<>();
                pagerInfo.setTotalCount(page.getTotal());
                pagerInfo.setItems(nullList);
                return pagerInfo;
            }
            if (location!=null){
                locId=location.getLocId();
            }
        }
        Page<InvInfoMainResponse> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
        List<InvInfoMainResponse> datas = invInfoMapper.searchMainInfo(dto, poNo, locId, flag);
        PagerInfo<List<InvInfoMainResponse>> pagerInfo = new PagerInfo<>();
        pagerInfo.setTotalCount(page.getTotal());
        if (CollectionUtil.isEmpty(datas)){
            pagerInfo.setItems(nullList);
            return pagerInfo;
        }
        for (InvInfoMainResponse data : datas) {
            if (data!=null){
                if (data.getQaStatus()!=null){
                    QaStatusEnum qaStatusEnum = QaStatusEnum.getEnumByCode(data.getQaStatus());
                    if (qaStatusEnum!=null){
                        data.setQaStatusStr(qaStatusEnum.enumName);
                    }
                }
                if (data.getInvAttr()==null){
                    data.setInvAttr(InvAttrEnum.S.getCode());
                }
            }
        }
        pagerInfo.setItems(datas);
        return pagerInfo;
    }

    //查询库存详情
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public InvInfoDtlResponse searchInvDtl(InvInfoRequest dto){
        String ownerName=null;
        dto=this.setSearch(dto);
        if (dto!=null){
            if (dto.getOwner()!=null){
                DataItemDetailDTO dataItem=new DataItemDetailDTO();
                List<DataItemDetailDTO> dataItemDetailByCode = dataItemDetailApi.getDataItemDetailByCode(DEFAULT_OWNER);
                for (DataItemDetailDTO dataItemDetailDTO : dataItemDetailByCode) {
                    if (DEFAULT_OWNER.equals(dataItemDetailDTO.getItemCode())){
                        dataItem=dataItemDetailDTO;
                    }
                }
                if (dataItem!=null){
                    if (dto.getOwner().equals(Integer.parseInt(dataItem.getItemValue()))){
                        ownerName=dataItem.getItemName();
                    }
                }
            }
        }
        InvInfoDtlResponse result = invInfoMapper.selectNewInvDtl(dto);
        if (result!=null){
            result.setOwnerName(ownerName);
            if (result.getQaStatus()!=null){
                QaStatusEnum qaStatusEnum = QaStatusEnum.getEnumByCode(result.getQaStatus());
                if (qaStatusEnum!=null){
                    result.setQaStatusStr(qaStatusEnum.enumName);
                }
            }
        }


        return result;
    }

    //查询SN列表信息
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public  List<InvInfoSnResponse> searchInvSnList(InvInfoRequest dto){
        dto=this.setSearch(dto);
        List<InvInfoSnResponse> datas = invInfoMapper.selectInvSnList(dto);
        for (InvInfoSnResponse data : datas) {
            if (data.getStatus()!=null){
                GoodsStatusEnum goodsStatusEnum = GoodsStatusEnum.getEnumByCode(data.getStatus());
                if (goodsStatusEnum!=null){
                    data.setStatusStr(goodsStatusEnum.enumName);
                }
            }
        }
        return datas;
    }

    //前端传-1，则设定值为null
    private InvInfoRequest setSearch(InvInfoRequest dto){
        Integer integer=new Integer(-1);
        BigDecimal bigDecimal=new BigDecimal(-1);
        String str="-1";
        if (bigDecimal.equals(dto.getSkuId())){
            dto.setSkuId(null);
        }
        if (str.equals(dto.getPoNo())){
            dto.setPoNo(null);
        }
        if (str.equals(dto.getSoNo())){
            dto.setSoNo(null);
        }
        if (str.equals(dto.getSoItem())){
            dto.setSoItem(null);
        }
        if (integer.equals(dto.getQaStatus())){
            dto.setQaStatus(null);
        }
        if (integer.equals(dto.getWhsId())){
            dto.setWhsId(null);
        }
        if (integer.equals(dto.getLocId())){
            dto.setLocId(null);
        }
        if (str.equals(dto.getFactory())){
            dto.setFactory(null);
        }
        if (str.equals(dto.getLotno())){
            dto.setLotno(null);
        }
        if (integer.equals(dto.getOwner())){
            dto.setOwner(null);
        }
        if (integer.equals(dto.getVendorId())){
            dto.setVendorId(null);
        }
        if (str.equals(dto.getInvAttr())){
            dto.setInvAttr(null);
        }
        if (InvAttrEnum.S.getCode().equals(dto.getInvAttr())){
            dto.setInvAttr(null);
        }
        return dto;
    }


}
